use serde::Deserialize;
use serde::Serialize;
use std::fmt::Debug;
use ts_rs::TS;
use yata::core::PeriodType;
use yata::core::ValueType;

///
/// 参见[SMA](https://docs.rs/yata/latest/yata/methods/struct.SMA.html) [EMA](https://docs.rs/yata/latest/yata/methods/struct.EMA.html) 等
///
///
#[derive(Debug, Serialize, Deserialize, Default, TS, Clone, PartialEq)]
#[ts(export)]
pub struct MA<T> {
    pub values: Vec<PeriodV<T>>,
}

impl<T> MA<T> where T: Copy + Debug {
    /// Finds the moving average for the specified period.
    /// Returns the moving average value if found, otherwise None.
    pub fn mav(&self, period: u16) -> Option<T> {
        self.values.iter().find_map(|pma| {
            if pma.period == period { Some(pma.value.clone()) } else { None }
        })
    }
}

#[derive(Debug, Serialize, Deserialize, Default, TS, Clone, PartialEq)]
#[ts(export)]
pub struct PeriodV<T> {
    /// ma period
    pub period: u16,
    /// Ma value
    pub value: T,
}

/// pub fn tan(&self) -> ValueType
/// Returns tangent of the current state line
///
/// pub fn b(&self) -> ValueType
/// Returns current value
///
#[derive(Serialize, Deserialize, Default, Debug, TS, Copy, Clone)]
#[ts(export)]
pub struct LSMAv {
    /// Returns tangent of the current state line
    pub tan: ValueType,
    /// Returns current value
    pub b: ValueType,
}

/// MA 周期设置，支持多条均线
#[derive(Serialize, Deserialize, Debug, TS, Clone)]
#[ts(export)]
pub struct MAConfig {
    /// MA 周期设置，支持多条均线
    pub periods: Vec<u8>,
}

impl Default for MAConfig {
    fn default() -> Self {
        Self {
            periods: vec![5, 10, 21, 30, 60, 72, 89, 120, 250],
        }
    }
}
